<template>
	<view class="">
		<view class="search-box">
			<u-search placeholder="请输入疗程卡名称" shape="square" :showAction="false" v-model="searchData.name" @search="onSearch"></u-search>
		</view>
		<view class="my-list mt-20 bg-white">
			 <u-list :height="swiperHeight" @scrolltolower="scrolltolower">
				<view class="">
					<view class="sety-num" v-for="(goIm, goIx) in dataList" :key="goIx">
						<image class="img" :src="goIm.image || '../../static/logo.png'" />
						<view class="tbx">
							<text>{{goIm.name}}</text>
							<view class="flex justify-between align-center">
								<view class="line-red">
									<text class="fsize-22">￥</text>
									<text class="fsize-32">{{goIm.salePrice}}</text>
								</view>
								<u-number-box
									v-model="goIm.nums"
									step="1"
									button-size="26"
									:min="0"
									:showMinus="goIm.nums > 0">
									<view
										slot="input"
										style="width: 50px;text-align: center;"
										class="input"
									>
										<text v-if="goIm.nums > 0">{{goIm.nums}}</text>
									</view>
								</u-number-box>
							</view>
						</view>
					</view>
				</view>
			</u-list>
		</view>
		<!-- 无数据时默认显示 -->
		<view class="emptybox" v-if="dataList.length === 0">
			<u-empty icon-size="150" text="暂无数据" mode="data"></u-empty>
		</view>
		<u-gap height="80"></u-gap>
		<view class="bottom-button">
			<u-button type="primary" size="large" color="#e54d42" text="确认选择" @click="onNavigateBack"></u-button>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import listHeightMixin from '@/common/computedAltitude.js'
	export default {
		mixins:[listHeightMixin],
		data() {
			return {
				loadmore: {
					status: 'loadmore',
					iconType: 'flower',
					loadText: {
						loadmore: '轻轻上拉',
						loading: '努力加载中',
						nomore: '实在没有了'
					},
				},
				searchData: {
					name: '',
					page: 1,
					limit: 999,
					staffId: 0,
				},
				dataList: [],
				checkeds: [],
				newCheckeds:[],
				nels:[],
			}
		},
		onLoad(options) {
			if(options.items){
				this.nels = JSON.parse(options.items)
			} 
			this.getPageList()
		},
		methods: {
			newArr(arr1, arr2){
				const map = new Map(arr1.map(item => [item.itemId, item])); // 将 arr1 转换为 Map
				arr2.forEach(item => {
				  const existing = map.get(item.itemId);
				  if (existing) {
				    map.set(item.itemId, {...existing, ...item}); // 如果存在，则用第二个数组中的元素替换第一个数组中的元素
				  }
				});
				const result = [...map.values()]; // 将 Map 中的值转换为数组
				return result
			},
			onProject(row){
				row.selected = !row.selected
				this.$reqJsonPost('GetProjectsDoSetSelected', {... row, staffId: this.searchData.staffId}).then(res=>{
					uni.$emit('upList', 2)
				})
			},
			checkboxChange(arr) {
				this.newCheckeds = arr
			},
			onNavigateBack(){
				const newArr = this.dataList.filter(om=>om.nums !== 0)
				if(newArr.length > 0){
					uni.$emit('add-pct', newArr)
					uni.navigateBack()
				}else {
					uni.showToast({
						title: '请选择疗程卡',
						icon: 'none',
						duration: 2000
					})
				}
			},
			// 搜索事件
			onSearch() {
				this.searchData.page = 1
				this.dataList = []
				this.getPageList()
			},
			getPageList() {
				this.$reqPost('ProjectCategoryCardItemList', this.searchData).then(res => {
					let list = res.data.map(im=>{
						im.nums = 0
						im.itemId = im.id
						return im
					})
					this.dataList = [...this.dataList, ...res.data]
					this.dataList = this.newArr(this.dataList, this.nels)
					if (res.count > this.dataList.length) {
						this.loadmore.status = 'loadmore';
						this.searchData.page++;
					} else {
						// 数据已加载完毕
						this.loadmore.status = 'nomore';
					}
				})
			},
			scrolltolower() {
				if (this.loadmore.status === 'loadmore') {
					this.getPageList()
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.item {
		padding: 20rpx;
		font-size: 28rpx;
		border-bottom: 1px #f5f5f5 solid;
	}
	.sety-num{
		padding: 20rpx;
		display:flex;
		border-bottom: 1px #f7f7f7 solid;
		.img{
			width: 100rpx;
			height: 100rpx;
			border-radius: 10rpx;
			flex-shrink: 0;
			margin-right: 10px;
		}
		.tbx{
			width: 100%;
			display:flex;
			flex-direction: column;
			justify-content: space-between;
		}
	}
</style>
